clear 
clear matrix
clear mata
set more off
set varabbrev off
set mem 100000000

* path to main folder for Data
global mystart  "Replication JPE\Data"

global out_figures "${mystart}\Tables and Figures\"
global out_tables "${mystart}\Tables and Figures\"

cd "${mystart}\Simulated Data\Experiments Minorities\Do Files\"


qui{
//do "Import Experiments Data.do" // uncomment this if re-running the experiments and needing to re-import the data into STATA
}

cd "${mystart}\Simulated data\Experiments Minorities\Experiments Data/"


* Table 10 in paper
qui{
* 1. first calculate effects of sick days on earnings - will need this in the second step
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0

gen lost_earnings= (40-hours)* wage_offer*52 if offer_accepted==4 | offer_accepted==5
replace lost_earnings= (20-hours)* wage_offer*52 if (offer_accepted==2 | offer_accepted==3) 


gen earnings_total = earnings + lost_earnings
replace earnings_total =0 if earnings_total==.
keep if age<65
replace sick_days=0 if offer_accepted==1 | offer==1 | offer==.

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period) 
 
gen discounted_earnings_total=earnings_total*discounting_factor
bysort experiment ID: egen earnings_PV_total = total(discounted_earnings_total)
keep if age==25

rename earnings_PV_total earnings_PV

preserve
inequal7 earnings_PV if experiment==0
gen COV=r(cov) if experiment==0
gen GINI=r(gini) if experiment==0
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI , by(experiment) 
gen education=0
sort education experiment
save "Temp\PV Earnings Variation SD.dta", replace
restore 

* by race

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ra of numlist  2 3 {
inequal7 earnings_PV if experiment==0 &  race==`ra'
replace COV=r(cov) if experiment==0 & race==`ra'
replace GINI=r(gini) if experiment==0 & race==`ra'
}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment race) 
sort race experiment
reshape wide MEAN COV  GINI , i(race) j(experiment)
save "Temp\PV Earnings Variation SD.dta", replace
restore


*** BY PRODUCTIVITY
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ra of numlist  2 3 {
foreach prod of numlist 1 2 3  {
cap inequal7 earnings_PV if experiment==0 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==0 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==0 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==0 & race==`ra' & productivity_type==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment race productivity_type) 
sort race experiment productivity_type
reshape wide MEAN COV  GINI , i(race productivity_type) j(experiment)
append using "Temp\PV Earnings Variation SD.dta"
rename MEAN0 MEAN6 
rename COV0 COV6
rename GINI0 GINI6
replace productivity_type=0 if productivity_type==.
sort productivity_type race
gen order= 1 if 	race==2 & productivity_type==0

replace order= 2 if 	race==2 & productivity_type==1
replace order= 3 if 	race==2 & productivity_type==2
replace order= 4 if 	race==2 & productivity_type==3

replace order= 5 if 	race==3 & productivity_type==0
replace order= 6 if 	race==3 & productivity_type==1
replace order= 7 if 	race==3 & productivity_type==2
replace order= 8 if 	race==3 & productivity_type==3
drop race productivity_type
sort order
save "Temp\PV Earnings Variation SD.dta", replace

}

* 2. prepare data from all relevant experiments
qui{

* table with pv earnings inequality, benchmark, no shocks - decision rules fixed, no shocks - decision rules adjust.
clear 
use "Experiments Stata Files/Experiment021.dta"
append using "Experiments Stata Files/Experiment05.dta"
append using "Experiments Stata Files/Experiment022.dta"
append using "Experiments Stata Files/Experiment024.dta"
append using "Experiments Stata Files/Experiment028.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"

replace experiment=0 if experiment==21 // Benchmark
replace experiment=1 if experiment==22 // eliminate shocks, decision rules fixed, same LS and savings as benchmark
replace experiment=2 if experiment==28 // eliminate shocks, decision rules fixed, same HC as benchmark
replace experiment=3 if experiment==24 // eliminate shocks, decision rules fixed, same wage offers as benchmark
replace experiment=6 if experiment==4 // eliminate shocks, lucky, decision rules change

replace experiment=4 if experiment==5 // eliminate shocks, decision rules fixed
replace experiment=5 if experiment==6 // eliminate shocks, lucky, decision rules change

keep if age<=64

// eliminate everyone in experiment 22 who didn't live in benchmark
sort ID age experiment
drop if experiment==1 & age!=age[_n-1]

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
keep if age==25

preserve
inequal7 earnings_PV if experiment==0
gen COV=r(cov) if experiment==0
gen GINI=r(gini) if experiment==0
inequal7 earnings_PV if experiment==1
replace COV=r(cov) if experiment==1
replace GINI=r(gini) if experiment==1
inequal7 earnings_PV if experiment==2
replace COV=r(cov) if experiment==2
replace GINI=r(gini) if experiment==2
inequal7 earnings_PV if experiment==3
replace COV=r(cov) if experiment==3
replace GINI=r(gini) if experiment==3
inequal7 earnings_PV if experiment==4
replace COV=r(cov) if experiment==4
replace GINI=r(gini) if experiment==4
inequal7 earnings_PV if experiment==5
replace COV=r(cov) if experiment==5
replace GINI=r(gini) if experiment==5
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI , by(experiment) 
gen education=0
sort education experiment
save "Temp\PV Earnings Variation.dta", replace
restore 

* by race

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ra of numlist  2 3 {
inequal7 earnings_PV if experiment==0 &  race==`ra'
replace COV=r(cov) if experiment==0 & race==`ra'
replace GINI=r(gini) if experiment==0 & race==`ra'
inequal7 earnings_PV if experiment==1 & race==`ra'
replace COV=r(cov) if experiment==1 & race==`ra'
replace GINI=r(gini) if experiment==1 & race==`ra'
inequal7 earnings_PV if experiment==2 & race==`ra'
replace COV=r(cov) if experiment==2 & race==`ra'
replace GINI=r(gini) if experiment==2 & race==`ra'

inequal7 earnings_PV if experiment==3 & race==`ra'
replace COV=r(cov) if experiment==3 & race==`ra'
replace GINI=r(gini) if experiment==3 & race==`ra'

inequal7 earnings_PV if experiment==4 & race==`ra'
replace COV=r(cov) if experiment==4 & race==`ra'
replace GINI=r(gini) if experiment==4 & race==`ra'

inequal7 earnings_PV if experiment==5 & race==`ra'
replace COV=r(cov) if experiment==5 & race==`ra'
replace GINI=r(gini) if experiment==5 & race==`ra'

}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment race) 
sort race experiment
reshape wide MEAN COV  GINI , i(race) j(experiment)
save "Temp\PV Earnings Variation.dta", replace
**** OPEN THS FILE TO COPY PASTE INTO EARNINGS INEQUALITY TABLE
restore


*** BY PRODUCTIVITY
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ra of numlist  2 3 {
foreach prod of numlist 1 2 3  {
cap inequal7 earnings_PV if experiment==0 &  race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==0 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==0 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==0 & race==`ra' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==1 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==1 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==1 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==1 & race==`ra' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==2 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==2 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==2 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==2 & race==`ra' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==3 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==3 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==3 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==3 & race==`ra' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==4 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==4 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==4 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==4 & race==`ra' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==5 & race==`ra' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==5 & race==`ra' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==5 & race==`ra' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==5 & race==`ra' & productivity_type==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment race productivity_type) 
sort race experiment productivity_type
reshape wide MEAN COV  GINI , i(race productivity_type) j(experiment)
append using "Temp\PV Earnings Variation.dta"
save "Temp\PV Earnings Variation.dta", replace

**** OPEN THS FILE TO COPY PASTE INTO EARNINGS INEQUALITY TABLE
clear 
use "Temp\PV Earnings Variation.dta"
replace productivity_type=0 if productivity_type==.

sort productivity_type race
gen order= 1 if 	race==2 & productivity_type==0
replace order= 2 if 	race==2 & productivity_type==1
replace order= 3 if 	race==2 & productivity_type==2
replace order= 4 if 	race==2 & productivity_type==3

replace order= 5 if 	race==3 & productivity_type==0
replace order= 6 if 	race==3 & productivity_type==1
replace order= 7 if 	race==3 & productivity_type==2
replace order= 8 if 	race==3 & productivity_type==3
sort order
merge order using "Temp\PV Earnings Variation SD.dta" // merge with file we saved in step 1. 
drop _merge

save "Temp\PV Earnings Variation.dta", replace
}

* 3. Construct Table 4
qui{
clear 
use "Temp\PV Earnings Variation.dta"

* need to infer some effects from differences across experiments 
gen MEAN7=MEAN0 - ( (MEAN0- MEAN4 )- (MEAN0 - ( MEAN0-(MEAN6 - MEAN1))) - (MEAN0-( MEAN0-(MEAN2 - MEAN4))) )
gen MEAN8=MEAN0 -(MEAN2 - MEAN4)
gen MEAN9= MEAN0-(MEAN6-MEAN1)

keep race productivity_type MEAN0 MEAN7 MEAN8 MEAN9 MEAN4 MEAN5
order race productivity_type MEAN0 MEAN7 MEAN8 MEAN9 MEAN4 MEAN5

replace MEAN7=(MEAN7-MEAN0)/MEAN0*100
replace MEAN8=(MEAN8-MEAN0)/MEAN0*100
replace MEAN9=(MEAN9-MEAN0)/MEAN0*100
replace MEAN4=(MEAN4-MEAN0)/MEAN0*100
replace MEAN5=(MEAN5-MEAN0)/MEAN0*100

replace MEAN4= MEAN5-MEAN4 // behavioral effect

rename MEAN0 col1
rename MEAN7 col2
rename MEAN8 col3 
rename MEAN9 col4
rename MEAN4 col5
rename MEAN5 col6

* OPEN DATA AND FIND TABLE 10 - copy paste. all columns and rown in order
}
* OPEN DATA AND FIND TABLE 10 - copy paste. all columns and rown in order
}

* Table 131 in Appendix
* Health spending by source
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if age<65
keep if race==2
*generate table as follows:
*total MC*.6 / person
*total MC*6 if treated / person 
* paid by OOP/person 
* paid by ESHI
* paid by Public 
* Unpaid


* OOP paid by individuals
gen col3= OOP if tr_pay_alt==1 
replace col3=OOP if type2==1 & govt_transfer==0  
replace col3= OOP-govt_transfer if govt_transfer<OOP & govt_transfer>0 & tr_pay_alt==2

* What ESHI pays
gen col4= MC*.6 - OOP if I_treat==1 & ESHI==1 


* Public: Medicaid and DI (not the Universal Health insurance)
gen col5=OOP  if tr_pay_alt==2 
* Medicaid only pays some of OOP when the person can afford the rest
replace col5 =  govt_transfer if govt_transfer<OOP &  tr_pay_alt==2

* Unpaid bills
gen col6 =OOP if tr_pay_alt==3 


* Costs if all treated
gen col7=OOP if tr_pay_alt==4 & experiment==0 & ESHI==0 
replace col7=MC_orig*.6 if tr_pay_alt==4 & experiment==0 & ESHI==1  
replace col7=MC_orig*.6  if tr_pay_alt==4 & experiment==14 

* Universal health insurance 
gen col8=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14
gen col9=MC_orig *.6 - OOP if (I_treat==1 & ESHI==0)  & experiment==14

gen count=1

* Calculate statistics for all, for benchmark and UHI experiment
preserve
collapse (sum) col*  count, by(experiment )
replace col3=col3/count
replace col4=col4/count
replace col5=col5/count
replace col6=col6/count
replace col7=col7/count
replace col8=col8/count
replace col9=col9/count
drop count
save "Temp/Bills_All_exp.dta", replace
restore

* Calculate statistics by ESHI/ No ESHI groups
collapse (sum) col*  count, by(experiment ESHI)
replace col3=col3/count
replace col4=col4/count
replace col5=col5/count
replace col6=col6/count
replace col7=col7/count
replace col8=col8/count
replace col9=col9/count

drop count
append using "Temp/Bills_All_exp.dta"

label var col3 "OOP (Self)"
label var col4 "ESHI"
label var col5 "Medicaid"
label var col6 "Unpaid"
label var col7 "Untreated"
label var col8 "Public Insurance"
label var col9 "Public Insurance - No Preventive"

gen total= col3 + col4 + col5 + col6 if experiment==0
replace total= col3 + col4 + col5 + col6 + col8 if experiment==14
label var total "Total if treated"

replace ESHI = 2 if ESHI==.
label define eshi_new 0 "No ESHI" 1 "ESHI" 2 "All"
label values ESHI eshi_new


table  (ESHI experiment) ,  statistic(mean col3 col4 col5 col6 col8 col9 total col7)  nototals nformat(%5.0f)  

collapse (mean) col3 col4 col5 col6 col8 col9 total col7, by (ESHI experiment)
sort experiment ESHI 
drop col9
order experiment ESHI  total col3 col4 col5 col6 col8  col7

* Open data and copy paste in Table 131. All columns and rows are in order
}


* Table 133 in Appendix 
qui{
* First panel: Average expenses
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if age<65
keep if race==2
*generate table as follows:
*total MC*.6 / person
*total MC*6 if treated / person 
* paid by OOP/person 
* paid by ESHI
* paid by Public 
* Unpaid


* OOP paid by individuals
gen col3= OOP if tr_pay_alt==1 
replace col3=OOP if type2==1 & govt_transfer==0  
replace col3= OOP-govt_transfer if govt_transfer<OOP & govt_transfer>0 & tr_pay_alt==2

* What ESHI pays
gen col4= MC*.6 - OOP if I_treat==1 & ESHI==1 


* Public: Medicaid and DI (not the Universal Health insurance)
gen col5=OOP  if tr_pay_alt==2 
* Medicaid only pays some of OOP when the person can afford the rest
replace col5 =  govt_transfer if govt_transfer<OOP &  tr_pay_alt==2

* Unpaid bills
gen col6 =OOP if tr_pay_alt==3 


* Costs if all treated
gen col7=OOP if tr_pay_alt==4 & experiment==0 & ESHI==0 
replace col7=MC_orig*.6 if tr_pay_alt==4 & experiment==0 & ESHI==1  
replace col7=MC_orig*.6  if tr_pay_alt==4 & experiment==14 

* Universal health insurance 
gen col8=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14
gen col9=MC_orig *.6 - OOP if (I_treat==1 & ESHI==0)  & experiment==14

gen count=1

* Calculate statistics for all, for benchmark and UHI experiment
collapse (sum) col*  count, by(experiment)
replace col3=col3/count
replace col4=col4/count
replace col5=col5/count
replace col6=col6/count
replace col7=col7/count
replace col8=col8/count
replace col9=col9/count
drop count


label var col3 "OOP (Self)"
label var col4 "ESHI"
label var col5 "Medicaid"
label var col6 "Unpaid"
label var col7 "Untreated"
label var col8 "Public Insurance"
label var col9 "Public Insurance - No Preventive"

gen total= col3 + col4 + col5 + col6 if experiment==0
replace total= col3 + col4 + col5 + col6 + col8 if experiment==14
label var total "Total if treated"

table  (experiment) ,  statistic(mean col3 col4 col5 col6 col8 col9 total col7)  nototals nformat(%5.0f)  

collapse (mean) col3 col4 col5 col6 col8 col9 total col7, by (experiment)
sort experiment 
drop col9 
order experiment  col8  col3 col4 col5 col6  total  col7

* Open data and copy paste in Table 8. All columns and rows are in order

sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Second panel: Health outcomes
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

keep if age<65
keep if race==2

gen I_shock=0 
replace I_shock=1 if type2!=1 

gen I_Good=0 
replace I_Good=1 if H==3

* these are the numbers in panel 2
table experiment, stat(mean  I_treat I_shock I_Good max_age)

collapse (mean) I_treat I_shock I_Good max_age , by (experiment)

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 2 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Third panel: Labor Market outcomes
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if race==2

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

rename education college_alt
sort age college_alt
merge age college_alt using "${mystart}\CPS\CPS Data Files\Intermediate Data Files/Equiv.dta"

rename college_alt education
replace equiv= 1.5 if age>64
replace equiv= 1.5 + (equiv -2.0 )*0.3 if age<65

gen family_consumption = consumption
replace consumption = consumption/ equiv if married==1

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_family_consumption = family_consumption*discounting_factor
bysort experiment ID: egen family_consumption_PV = total(discounted_family_consumption)
replace family_consumption_PV=. if age!=25
replace family_consumption_PV=family_consumption_PV/1000


gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000

* FT yrs of work
bysort experiment ID: egen yrs_work=max(human_capital_prime) 
replace  yrs_work=. if age!=25 // want only 1 obs per ID

* these are the numbers in panel 3
table experiment if age<65, stat(mean wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR) stat(cv family_consumption_PV earnings_PV)

* construct table for convenience
keep if age<65
collapse (mean) wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR (sd) CV_cons=family_consumption_PV CV_earn=earnings_PV , by (experiment)

replace CV_cons =CV_cons/family_consumption_PV // calculate coeff of variation for consumption
replace CV_earn =CV_earn/earnings_PV // calculate coeff of variation for earnings

order experiment wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 3 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Fourth panel: Gov revenue and expenditures per working age male
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if race==2
keep if age<65

* revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) 

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
gen gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK

* for those with ESHI
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65

gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65


collapse (mean) Total_tax UHI_premium UHI govt_transfer  Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)

sort experiment
rename Total_tax Total_tax1
rename govt_transfer govt_transfer1
rename Unpaid_bills Unpaid_bills1
rename gov_expenditure gov_expenditure1
merge experiment using "Temp\Table_UHI.dta"
drop _merge	
sort experiment
save "Temp\Table_UHI.dta", replace	
}


* Fifth panel: Gov revenue and expenditures per HH
qui{
clear 
use "Experiments Stata Files/Experiment021.dta"
replace experiment=0 if experiment==21
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if race==2
* how much revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5)

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI: benchmark and experiment the same
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

* now let's fix the 65+
replace expenditures=0 if age>64
replace Income_tax=0 if age>64 
replace Cons_tax=0 if age>64
replace Total_tax=0 if age>64

replace expenditures = Expenditures_pay if age>64
gen Mcare_premium = 854  if age>64
replace Mcare_premium = Mcare_premium + 854 if age>64 & married==1 
replace Mcare_premium =0 if age<65

replace Income_tax=expenditures - Mcare_premium - OOP if age>64 & married==0
replace Income_tax=expenditures - Mcare_premium - OOP - OOP_spouse  if age>64 & married==1

replace Cons_tax = consumption * .057 if age>64
replace Total_tax = Income_tax + Cons_tax if age>64

replace SS_income =0 if age<65
gen total_SS_income = SS_income + spouse_income if age>64 & married==1
replace total_SS_income = SS_income if age>64 & married==0
replace total_SS_income =0 if age<65

gen Mcare_bills= 0 if age<65
replace Mcare_bills=OOP if age>64 & married==0
replace Mcare_bills=OOP + OOP_spouse if age>64 & married==1

* USE BUDGET CONSTRAINT TO INFER TOTAL GOVT EXPENDITURES FOR EACH PERSON. THIS WILL BE THE SUM OF EVERYTHING
* 65+ PEOPLE
* A' + C -RA + ME = SS - MEDICARE_PREM - INCOME_TAX - C(TAX) + ME (1-MEDICARE_COPAY) + TR
* EVERYTHING ON THE RIGHT HAND SIDE IS GOVT TRANSFERS AND REVENUE FROM THIS THIS PERSON
gen gov_expenditure= assets_prime + consumption + OOP*2 + OOP_spouse*2 - 1.045*assets if age>=65 // cons is after tax. OOP*2 is the total medical expendiure of the old. Medicare pays half by assumption. 

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* for those <65. 
* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK
* the following 3 lines are for those with ESHI, which are the same in benchmark and UHI experiment, so we run them for both. If anyone is also on Medicaid, that's OK because Medicaid would only pay the OOP part after ESHI paid.
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works



* GENERATING THINGS FOR TABLE IN PAPER
* BOTTOM PANEL
* TAX REVENUE ; SS+medicare - medicare premiums 
gen SS_Medicare =Mcare_premium -Mcare_bills-total_SS_income


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65
* in the benchmark, for those with ESHI, the unpaid part is only the OOP (ESHI always pays the rest). for those without ESHI, OOP is set to .6*MC. 
* in the experiment, it's the same for those with ESHI. If no ESHI, the unpaid part is just the OOP of the person. the rest gets paid by UHI

* UHI
gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65
gen UHI_total= UHI- UHI_premium

collapse (mean) Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)
sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

clear
use "Temp\Table_UHI.dta"

order experiment col8  col3 col4 col5 col6  total  col7 I_treat I_shock I_Good max_age  wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR Total_tax1 UHI_premium UHI govt_transfer1  Unpaid_bills1 gov_expenditure1  Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure

xpose, clear
drop if v1==0 & v2==14
rename v1 Benchmark 
rename v2 Public_HI

* OPEN DATA -- COPY PASTE INTO TABLE 133 - NUMBERS IN COLUMNS ARE IN ORDER (ALL PANELS COMBINED)
}